package com.dooioo.fangyou.service.impl;

import com.dooioo.fangyou.common.util.BindParams;
import com.dooioo.fangyou.dao.QueryDao;
import com.dooioo.fangyou.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * 业务层基本实现类
 * User: kqy
 * Date: 2010-11-30
 * Time: 15:07:48
 */

public class SpringBaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    protected QueryDao<T> queryDao;
    
    private Class<T> poClass;
	private String poClassName;

    public void setQueryDao(QueryDao queryDao) {
        this.queryDao = queryDao;
    }

    public SpringBaseServiceImpl() {
        poClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        poClassName = poClass.getSimpleName();
	}

    public T findById(String id) {
        return query(new BindParams().put(tableId(), id).getParams());
    }

    public T findById(String sqlId, String id) {
        return null; 
    }

    public T findBy(String column, String param) {
        return query(new BindParams().put(column, param).getParams());
    }

    public T findBy(String sqlId, String column, String param) {
        return null;
    }

    public List<T> queryForList() {
        return queryDao.queryForList(findAlias());  
    }

    public List<T> queryForList(String sqlId) {
        return queryForList(sqlId, null) ;
    }

    public int insert(T entityObject) {
        return queryDao.insert(findAlias(), entityObject);
    }

    public int insert(String sqlId, T entityObject) {
        return 0;
    }

    public int insert(String sqlId, Map<String, Object> bindParams) {
        return 0;
    }

    public int update(T entityObject) {
        return queryDao.update(updateAlias(), entityObject);
    }

    public int update(String sqlId, T entityObject) {
        return queryDao.update(sqlId, entityObject);  
    }

    public int update(String sqlId, Map<String, Object> bindParams) {
        return 0;
    }

    public int delete(String id) {
        return queryDao.delete(deleteAlias(), id);
    }

    public int delete(String sqlId, T entityObject) {
        return 0; 
    }

    public int delete(String sqlId, Map<String, Object> bindParams) {
        return 0;
    }

    protected T query(Map<String, Object> param) {
        return (T)queryDao.queryForObject(getAlias(), param);
    }

    protected List<T> queryForList(String sqlId, Map<String, Object> param) {
        return queryDao.queryForList(sqlId, param);
    }

    protected String getAlias() {
        return new StringBuilder(poClassName + ".").append(QUERY_GET).append(poClassName).toString();
    }

    protected String insertAlias() {
        return new StringBuilder(poClassName + ".").append(QUERY_INSERT).append(poClassName).toString();
    }

    protected String updateAlias() {
        return new StringBuilder(poClassName + ".").append(QUERY_UPDATE).append(poClassName).toString();
    }

    protected String findAlias() {
        return new StringBuilder(poClassName + ".").append(QUERY_FIND).append(poClassName).toString();
    }

    protected String deleteAlias() {
        return new StringBuilder(poClassName + ".").append(QUERY_DELETE).append(poClassName).toString();
    }

    protected String tableId() {
        return poClassName + "Id";
    }
}
